package com.resume.aipeople.service.ai.asr;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 语音识别策略管理器
 * 负责管理所有可用的语音识别策略，并提供策略选择功能
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class SpeechRecognitionStrategyManager {
    
    private final List<SpeechRecognitionStrategy> strategies;
    
    /**
     * 根据策略名称获取策略
     * @param strategyName 策略名称
     * @return 语音识别策略
     */
    public SpeechRecognitionStrategy getStrategy(String strategyName) {
        if (strategyName == null || strategyName.isEmpty()) {
            return getDefaultStrategy();
        }
        
        return strategies.stream()
                .filter(strategy -> strategy.getStrategyName().equals(strategyName))
                .findFirst()
                .orElse(getDefaultStrategy());
    }
    
    /**
     * 获取默认策略（优先选择可用的策略）
     * @return 默认语音识别策略
     */
    public SpeechRecognitionStrategy getDefaultStrategy() {
        // 优先选择可用的策略
        SpeechRecognitionStrategy availableStrategy = strategies.stream()
                .filter(SpeechRecognitionStrategy::isAvailable)
                .findFirst()
                .orElse(null);
        
        if (availableStrategy != null) {
            log.info("使用可用的语音识别策略: {}", availableStrategy.getDisplayName());
            return availableStrategy;
        }
        
        // 如果没有可用的策略，返回第一个策略作为默认策略
        if (!strategies.isEmpty()) {
            SpeechRecognitionStrategy defaultStrategy = strategies.get(0);
            log.warn("没有可用的语音识别策略，使用默认策略: {}", defaultStrategy.getDisplayName());
            return defaultStrategy;
        }
        
        throw new RuntimeException("没有找到任何语音识别策略");
    }
    
    /**
     * 获取所有可用的策略信息
     * @return 策略信息映射
     */
    public Map<String, StrategyInfo> getAvailableStrategies() {
        return strategies.stream()
                .collect(Collectors.toMap(
                        SpeechRecognitionStrategy::getStrategyName,
                        strategy -> new StrategyInfo(
                                strategy.getStrategyName(),
                                strategy.getDisplayName(),
                                strategy.isAvailable()
                        )
                ));
    }
    
    /**
     * 获取所有策略名称列表
     * @return 策略名称列表
     */
    public List<String> getStrategyNames() {
        return strategies.stream()
                .map(SpeechRecognitionStrategy::getStrategyName)
                .collect(Collectors.toList());
    }
    
    /**
     * 检查策略是否存在
     * @param strategyName 策略名称
     * @return 是否存在
     */
    public boolean hasStrategy(String strategyName) {
        return strategies.stream()
                .anyMatch(strategy -> strategy.getStrategyName().equals(strategyName));
    }
    
    /**
     * 策略信息类
     */
    public static class StrategyInfo {
        private final String name;
        private final String displayName;
        private final boolean available;
        
        public StrategyInfo(String name, String displayName, boolean available) {
            this.name = name;
            this.displayName = displayName;
            this.available = available;
        }
        
        public String getName() {
            return name;
        }
        
        public String getDisplayName() {
            return displayName;
        }
        
        public boolean isAvailable() {
            return available;
        }
    }
}



